package com.sunshine.config;

import com.sunshine.security.CorsConfigFilter;
import com.sunshine.security.JwtAuthenticationEntryPoint;
import com.sunshine.security.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * Created by along on 4/20/18.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationEntryPoint entryPoint;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder builder) throws Exception {
        builder.userDetailsService(this.userDetailsService)
                .passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() {
        return new JwtAuthenticationTokenFilter();
    }

    @Bean
    public CorsConfigFilter corsConfigFilterBean() {
        return new CorsConfigFilter();
    }

    /**
     * auth d的配置
     *
     * @param httpSecurity HttpSecurity
     * @throws Exception 库中抛出的异常，库本身没有明确是什么类型的异常，
     *                   所以这里只能为不明确的Exception
     */
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .csrf().disable() // API不需要CSRF保护
                .exceptionHandling().authenticationEntryPoint(entryPoint) // 异常处理
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 不要session
                .and()
                .authorizeRequests()
                .antMatchers(
                        HttpMethod.GET,
                        "/",
                        "/admin",
                        "/*.html",
                        "/favicon.ico",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/**/*.*").permitAll() //静态资源不需要认证
                .antMatchers(
                        "/api/auth",
                        "/api/article/**",
                        "/api/information/**",
                        "/api/count/**").permitAll() // 登录和学生接口不需要认证
                .antMatchers(HttpMethod.OPTIONS,
                        "/**").permitAll() //允许所有options
                .anyRequest().authenticated(); // 其它的都需要认证
        httpSecurity.headers().cacheControl();

        httpSecurity.addFilterBefore(
                authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
        httpSecurity.addFilterBefore(corsConfigFilterBean(), JwtAuthenticationTokenFilter.class);
    }
}
